package demo07.Revoke.control;

import demo07.Revoke.command.Command;
import demo07.Revoke.nocommand.NoCommand;

//组件
public class RemoteControlWithUndo {
	Command[] onCommands;
	Command[] offCommands;
	Command undoCommand;//记录前一个命令，用于撤销
	//构造器
	public RemoteControlWithUndo(){
		onCommands = new Command[7];
		offCommands = new Command[7];
		Command noCommand = new NoCommand(); //空
		for(int i=0;i<7;i++) {//初始化七个空命令数组
			onCommands[i] = noCommand;
			offCommands[i] = noCommand;
		}
		undoCommand = noCommand;//一开始也没有前一个命令
	}
	//设值
	public void setCommand(int slot, Command onCommand, Command offCommand) {
		onCommands[slot] = onCommand;
		offCommands[slot] = offCommand;
	}
 
	public void onButtonWasPushed(int slot) {//开
		onCommands[slot].execute();
		undoCommand = onCommands[slot];//记录命令
	}
 
	public void offButtonWasPushed(int slot) {//关
		offCommands[slot].execute();
		undoCommand = offCommands[slot];//记录命令
	}
 
	public void undoButtonWasPushed() {//撤销
		undoCommand.undo();
	}
  
	public String toString() {
		StringBuffer stringBuff = new StringBuffer();
		stringBuff.append("\n------ 远程控制 -------\n");
		for (int i = 0; i < onCommands.length; i++) {
			stringBuff.append("[位置 " + i + "] " +"开启类"+ onCommands[i].getClass().getName()
				+ "   关闭类 " + offCommands[i].getClass().getName() + "\n");
		}
		return stringBuff.toString();
	}
}
